package com.atguigu.dga.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ThreadDemo3 {
    public static void main(String[] args) throws Exception {
        /*
        Runnable:1.0 古老线程类
            void run():没有返回值。不能抛出异常

            Callable:1.5
                v call() throws Exception
                    有返回值，可以抛出异常
         */
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        Callable<String> callable = new Callable<String>() {

            @Override
            public String call() throws Exception {
                Thread.sleep(5000);
                System.out.println(Thread.currentThread().getName() + "已经执行完毕，返回结果！");
                return "ok";
            }
        };

        /*
       代表线程将来结束时的结果
         */
        Future<String> future = executorService.submit(callable);

        /*
        如何知道线程已经运行结束？
            call()返回线程最后返回的结果
                是阻塞式

                future.get():也是阻塞的
                    Future中的方法更丰富

               希望获取Callable的返回值，调用Future.get(),而不是call()
         */
        String res = callable.call();

        /*
        while (!future.isDone()){
            System.out.println("线程还未执行结束....再探...");
            Thread.sleep(500);
        }
         */

        System.out.println("线程已经运行完了:" + res);

        System.out.println("main.....");
        executorService.shutdown();
    }
}
